<?php
//----------------------------------------------LOGIN
function checkUser($config,$uname,$pwd){
	$uname = strtolower($uname);
	$pwd = md5(strtolower($pwd));
	$db = new db($config);
	if (check_login($db,$uname,$pwd)) {
		$_SESSION['loginstatus'] = "Selamat datang ".$uname;
		echo "benar";
	} else {
		$_SESSION['loginstatus'] = "Username atau Password salah";
		echo "salah";
	}
	header('Location: '.$config['home_url']."backend");
}

function check_login($db,$username,$md5) {
	$retval = false;
	$result = $db->query("SELECT * FROM user WHERE username = '".urlencode($username)."' AND password = '$md5'");
	if ($result && ($db->rowCount() > 0)) {
		$retval = true;
		$_SESSION['logged']['username'] = $result[0]['username'];
		$_SESSION['logged']['role'] = $result[0]['role'];
		$_SESSION['logged']['idUser'] = $result[0]['idUser'];
	}
	return $retval;
}
//----------------------------------------------HOME
require('_bckhome.php');
//----------------------------------------------ABOUT
require('_bckabout.php');
//----------------------------------------------CSTUDIO
require('_bckcstudio.php');
//----------------------------------------------SEKOLAH
require('_bcksekolah.php');
//----------------------------------------------SANGGAR
require('_bcksanggar.php');
//----------------------------------------------YAYASAN
require('_bckyayasan.php');
//----------------------------------------------TOKO
require('_bcktoko.php');
//----------------------------------------------CONTACT
function edit_bckcontact($config,$param){
	$notelp = trim($param['notelp']);
	$email = trim($param['email']);
	$alamat = trim($param['alamat']);
	var_dump($param);
	$db = new db($config);
	$que = "UPDATE `kontak` SET `telp`=\"$notelp\", `email`=\"$email\", `alamat`=\"$alamat\" WHERE `page` = 'contact';";
	$db->exec($que);
	$_SESSION['editted'] = 'Sukses edit kontak';
	header('Location: '.$config['home_url']."backend/contact");
}
//----------------------------------------------FOOTER
function edit_bckfoot($config,$param){
	$fb = trim($param['fb']);
	$twitter = trim($param['twitter']);
	$alamat = trim($param['alamat']);
	$kontak = trim($param['kontak']);
	var_dump($param);
	$db = new db($config);
	$que = "UPDATE `footer` SET `facebook`=\"$fb\", `twitter`=\"$twitter\", `alamat`=\"$alamat\", `kontak`=\"$kontak\" WHERE `id` = 1;";
	$db->exec($que);
	$_SESSION['editted'] = 'Sukses edit footer';
	header('Location: '.$config['home_url']."backend/footer");
}
require('_bckusers.php');
//----------------------------------------------CONTROLLER
function controller_backend($config) {
    if(isset($_SESSION['logged'])){
		if (isset($config['ruri'][1]) && $config['ruri'][1]=='logout'){
			unset($_SESSION['logged']);
			unset($_SESSION['loginstatus']);
			$_SESSION['logoutstatus'] = "Anda telah keluar";
			header('Location: '.$config['home_url']."backend");
		}else{
			if (isset($config['ruri'][1])){
				switch($config['ruri'][1]){
					//----------------------------HOME
					case 'home' :
						view_bckhome($config,array());
						break;
					case 'edhome' :
						edit_bckhome($config,$_POST);
						break;
					case 'edkutip' :
						if (isset($_POST['tambah'])){
							add_bckkutip($config,$_POST);
						}
						break;
					case 'delkutip' :
						del_bckkutip($config,$_REQUEST);
						break;
					//----------------------------ABOUT
					case 'aboutus' :
						view_bckabout($config,array());
						break;
					case 'edaboutdesc' :
						edit_bckabout($config,$_POST);
						break;
					case 'edaboutangg' :
						if (isset($_POST['tambah'])){
							add_bckaboutangg($config,$_POST);
						}else if (isset($_POST['edit'])){
							edit_bckaboutangg($config,$_POST);
						}
						break;
					case 'delabout' :
						del_bckaboutangg($config);
						break;
					case 'edaboutfoto' :
						add_bckaboutfoto($config,$_POST);
						break;
					case 'delaboutfoto' :
						del_bckaboutfoto($config,$_REQUEST);
						break;
					case 'addaboutanggpp' :
						add_bckaboutanggpp($config,$_POST);
						break;
					case 'delaboutanggpp' :
						del_bckaboutanggpp($config,$_REQUEST);
						break;
					case 'addaboutanggfoto' :
						add_bckaboutanggfoto($config,$_POST);
						break;
					case 'delaboutanggfoto' :
						del_bckaboutanggfoto($config,$_REQUEST);
						break;
					//----------------------------CSTUDIO
					case 'cstudio' :
						view_bckcstudio($config,array());
						break;
					case 'edcstudio' :
						edit_bckcstudio($config,$_POST);
						break;
					case 'edcstudioworks' :
						if (isset($_POST['tambah'])){
							add_bckcstudioworks($config,$_POST);
						}else if (isset($_POST['edit'])){
							edit_bckcstudioworks($config,$_POST);
						}
						break;
					case 'edsubworks' :
						if (isset($_POST['tambah'])){
							add_bcksubworks($config,$_POST);
						}else if (isset($_POST['edit'])){
							edit_bcksubworks($config,$_POST);
						}
						break;
					case 'addcstudioworksfoto' :
						add_bckcstudioworksfoto($config,$_POST);
						break;
					case 'delcstudioworksfoto' :
						del_bckcstudioworksfoto($config,$_REQUEST);
						break;
					case 'delcstudioworks' :
						del_bckcstudioworks($config);
						break;
					case 'delsubworks' :
						del_bcksubworks($config);
						break;
					//----------------------------SEKOLAH
					case 'bimbel' :
						view_bckbimbel($config,array());
						break;
					case 'edbimbel' :
						edit_bckbimbel($config,$_POST);
						break;
					case 'edbimbelfoto' :
						add_bckbimbelfoto($config,$_POST);
						break;
					case 'delbimbelfoto' :
						del_bckbimbelfoto($config,$_REQUEST);
						break;
					case 'edbimbelkontak' :
						edit_bckbimbelkontak($config,$_POST);
						break;
					case 'edbimbelkeg' :
						if (isset($_POST['tambah'])){
							add_bckbimbelkeg($config,$_POST);
						}else if (isset($_POST['edit'])){
							edit_bckbimbelkeg($config,$_POST);
						}
						break;
					case 'edbimbelsub' :
						if (isset($_POST['tambah'])){
							add_bckbimbelsub($config,$_POST);
						}else if (isset($_POST['edit'])){
							edit_bckbimbelsub($config,$_POST);
						}
						break;
					case 'addbimbelkegfoto' :
						add_bckbimbelkegfoto($config,$_POST);
						break;
					case 'delbimbelkegfoto' :
						del_bckbimbelkegfoto($config,$_REQUEST);
						break;
					case 'delbimbelkeg' :
						del_bckbimbelkeg($config);
						break;
					case 'delbimbelsub' :
						del_bckbimbelsub($config);
						break;
					case 'edbimbeldl' :
						edit_bckbimbeldl($config,$_POST);
						break;
					case 'delbimbeldl' :
						del_bckbimbeldl($config,$_REQUEST);
						break;
					//----------------------------SANGGAR
					case 'sanggar' :
						view_bcksanggar($config,array());
						break;
					case 'edsanggar' :
						edit_bcksanggar($config,$_POST);
						break;
					case 'edsanggarkomu' :
						if (isset($_POST['tambah'])){
							add_bcksanggarkomu($config,$_POST);
						}else if (isset($_POST['edit'])){
							edit_bcksanggarkomu($config,$_POST);
						}
						break;
					case 'addsanggarkomufoto' :
						add_bcksanggarkomufoto($config,$_POST);
						break;
					case 'delsanggarkomufoto' :
						del_bcksanggarkomufoto($config,$_REQUEST);
						break;
					case 'delsanggarkomu' :
						del_bcksanggarkomu($config);
						break;
					case 'addsanggarkomupp' :
						add_bcksanggarkomupp($config,$_POST);
						break;
					case 'delsanggarkomupp' :
						del_bcksanggarkomupp($config,$_REQUEST);
						break;
					//--------------------------YAYASAN
					case 'yayasan' :
						view_bckyayasan($config,array());
						break;
					case 'edyayasan' :
						edit_bckyayasan($config,$_POST);
						break;
					case 'edyayasanfoto' :
						add_bckyayasanfoto($config,$_POST);
						break;
					case 'delyayasanfoto' :
						del_bckyayasanfoto($config,$_REQUEST);
						break;
					case 'edyayasankontak' :
						edit_bckyayasankontak($config,$_POST);
						break;
					//----------------------------PRODUK
					case 'produk' :
						view_bckproduk($config,array());
						break;
					case 'edtokolink':
						edit_bcktokolink($config,$_POST);
						break;
					case 'edtokokontak' :
						edit_bcktokokontak($config,$_POST);
						break;
					case 'addtokoprod' :
						add_bcktokoprod($config,$_POST);
						break;
					case 'deltokoprod' :
						del_bcktokoprod($config,$_REQUEST);
						break;
					case 'addtokospon' :
						add_bcktokospon($config,$_POST);
						break;
					case 'deltokospon' :
						del_bcktokospon($config,$_REQUEST);
						break;
					//--------------------------CONTACT
					case 'contact' :
						view_bckcontact($config,array());
						break;
					case 'edcontact' :
						edit_bckcontact($config,$_POST);
						break;
					//--------------------------FOOT
					case 'footer' :
						view_bckfoot($config,array());
						break;
					case 'edfoot' :
						edit_bckfoot($config,$_POST);
						break;
					//--------------------------USERS
					case 'pengguna' :
						view_bckuser($config,array());
						break;
					case 'eduser' :
						edit_bckuser($config,$_POST);
						break;
					case 'adduser' :
						add_bckuser($config,$_POST);
						break;
					case 'deluser' :
						del_bckuser($config,$_REQUEST);
						break;
					//-------------------------UTAMA
					default :
						view_backend($config,array());
						break;
				}
			}else
				view_backend($config,array());
		}
	}else{	//kalau belum login
		if (isset($_POST['submit'])){
			$uname = $_POST['uname'];
			$pwd = $_POST['pwd'];
			checkUser($config,$uname,$pwd);
		}else{
			view_login($config,array());
		}
	}
}
